//
//  ConfNoticeViewController.m
//  Conference
//
//  Created by 陈鑫 on 12-11-15.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//大会通知

#import "ConfNoticeViewController.h"
#import "AlarmData.h"
#import "NetContant.h"
#import "Globals.h"
#import "Constant.h"
#import "MainDataBase.h"
#import "WebData.h"
#import "DateTool.h"
#import "AlarmCellController.h"
#import "AlarmTimeCellController.h"
@interface ConfNoticeViewController ()

@end

@implementation ConfNoticeViewController
@synthesize table;
@synthesize arrayOfData;
//@synthesize tableHight;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}


- (void)viewDidLoad
{
    self.table.separatorStyle=NO;
    [self loadingData];
    [super viewDidLoad];
    
    
}


-(void)loadingData{
    self.arrayOfData=[NSMutableArray arrayWithCapacity:0];
    [self getDataFromDB];
//    NSThread *thread=[[[NSThread alloc] initWithTarget:self selector:@selector(run) object:nil] autorelease];
//    [thread start];
    
}

-(void)getDataFromDB{

    [self.arrayOfData removeAllObjects];
    
    NSMutableArray *alarmGroups=[[MainDataBase getInstance]getAlarmsGroup];

    NSDate *latsDate=nil;
    for (NSString *groupDate in alarmGroups) {
        NSDate *date=[[DateTool getInstance]getDateForString:groupDate];
        
        if(nil==latsDate){
            AlarmData *alarmData=[[AlarmData alloc]init];
            alarmData.isDate=YES;
            alarmData.alarmDate=[[DateTool getInstance]getTimeByDuration:groupDate];
            latsDate=date;
            [self.arrayOfData addObject:alarmData];
            [alarmData release];
        }else{
            
            NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
            unsigned int unitFlags = NSDayCalendarUnit;
            NSDateComponents *comps = [gregorian components:unitFlags fromDate:latsDate  toDate:date  options:0];
            int days = [comps day];
            [gregorian release];
            if (days!=0) {
                
                AlarmData *alarmData=[[AlarmData alloc]init];
                alarmData.isDate=YES;
                alarmData.alarmDate=[[DateTool getInstance]getTimeByDuration:groupDate];
                latsDate=date;
                [self.arrayOfData addObject:alarmData];
                [alarmData release];
            }
            
        }
        
        
     NSMutableArray *dateAlarmList=[[MainDataBase getInstance ]getAlarmsByDate:groupDate];

    [self.arrayOfData addObjectsFromArray:dateAlarmList];
    }
}
/*
-(void)run{
    
    NSAutoreleasePool *pool=[[NSAutoreleasePool alloc] init];
    NSString *sRet=[[WebData getInstance]getMemberAlarm];
    [self performSelectorOnMainThread:@selector(onPostExecute:) withObject:sRet waitUntilDone:YES];
    
	[pool release];
    
}

-(void)onPostExecute:(NSString *)sRet{
    
    if ([sRet isEqualToString:[[NetContant getInstance] NET_CONNECT_FALSE]]) {
        
        
       // [[Globals getInstance] showAlertView:[[Constant getInstance] net_error]];
        
        
    }else if([sRet isEqualToString:[[NetContant getInstance] NETWORK_RECORD_ISEMPTY]]){
        
       // [[Globals getInstance] showAlertView:[[Constant getInstance] data_isempty]];
        
    }else {
        @try {
            [self doNetRet:sRet];
            [self doNetInfo];
        }
        @catch (NSException *exception) {
           
          //  [[Globals getInstance] showAlertView:[[Constant getInstance] net_exception]];

        }
       
    }
    
}
-(void)doNetRet:(NSString *)sRet{
    NSArray *tables=[sRet componentsSeparatedByString:[[NetContant getInstance]TABLES_SPLIT_FLAG]];
        NSMutableArray *dataList=[NSMutableArray arrayWithCapacity:0] ;
            for (int i=0; i<[tables count]; i++) {
               if(nil!=[tables objectAtIndex:i]){
                   NSArray *record=[[tables objectAtIndex:i]componentsSeparatedByString:[[NetContant getInstance] RECORDS_SPLIT_FLAG]];
                   if([record count]>=2){
                       AlarmData *alarmData=[[AlarmData alloc]init];
                        alarmData.message=[record objectAtIndex:0];
                        alarmData.alarmDate = [record objectAtIndex:1];
                     
                        [dataList addObject:alarmData];
                       [ alarmData release];
                    }
               }
            }
    [[MainDataBase getInstance]addAlarms:dataList];
    [Globals getInstance].sAlarmUpdate=[NSString stringWithFormat:@"%@ %@",[[DateTool getInstance]getNowDate],[[DateTool getInstance]getNowTime]];
    
}
-(void)doNetInfo{
    [self getDataFromDB];
    [self.table reloadData];
//    self.table.frame=CGRectMake(0, 0, 320, self.tableHight);
}
*/

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AlarmData *data = [arrayOfData objectAtIndex:indexPath.row];
    if(data.isDate){
          UITableViewCell *cell = [self tableView:tableView cellForRowAtIndexPath:indexPath];
           return cell.frame.size.height;
    }else{
        CGSize size=[[Globals getInstance]CalculationCellSize:self.table.frame.size.width fontSize:16 content:data.message];
        return size.height+20;
    
    }
    
   
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.arrayOfData count];
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    AlarmData *data=[self.arrayOfData objectAtIndex:[indexPath row]];
    if(data.isDate){
        static NSString *alarmTimeCell=@"alarmTimeCell";
        AlarmTimeCellController *timeCell=(AlarmTimeCellController *)[tableView dequeueReusableCellWithIdentifier:alarmTimeCell];
        
        if(timeCell ==nil){
            NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"AlarmTimeCellController" owner:self options:nil];   
            for (id oneObject in array) {
                if ([oneObject isKindOfClass:[AlarmTimeCellController class]]) {
                    timeCell=oneObject;
                    break;
                }
            }
            timeCell.selectionStyle=UITableViewCellSelectionStyleNone;
        }
        
        timeCell.lab_time.text=data.alarmDate;
  //      self.tableHight=self.tableHight+20;
        return timeCell;
    }else {
        static NSString *norCell=@"norCell";
        
        AlarmCellController *cell=(AlarmCellController *)[tableView dequeueReusableCellWithIdentifier:norCell];
        if(cell ==nil){
            NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"AlarmCellController" owner:self options:nil];   
            for (id oneObject in array) {
                if ([oneObject isKindOfClass:[AlarmCellController class]]) {
                    cell=oneObject;
                    break;
                }
            }
            cell.selectionStyle=UITableViewCellSelectionStyleNone;

        }
        
        cell.lab_time.text=data.dateTime;
      
        cell.lab_title.text=data.title;
        
        UIFont *font = [UIFont systemFontOfSize:16];
        
       CGSize size=[[Globals getInstance]CalculationCellSize:self.table.frame.size.width fontSize:16 content:data.message];
        
        CGRect rect = [cell.lab_content textRectForBounds:cell.lab_content.frame limitedToNumberOfLines:0];
        rect.size=size;
        cell.lab_content.frame = rect;
        cell.lab_content.text=data.message;
        cell.lab_content.numberOfLines = 0;
        cell.lab_content.font = font;
        
        //设置时间在底部
        cell.lab_time.frame=CGRectMake(185, rect.size.height+40, 130, 20);
        
//        self.tableHight=self.tableHight+size.height+20;
        
        
//        UIImage *img=[UIImage imageNamed:@".png"];
//        [img stretchableImageWithLeftCapWidth:1 topCapHeight:1];
//        [cell.img_bg setFrame:CGRectMake(0, 0, 320, size.height+20)];
//        
//        [cell.img_bg setImage:img];
        
           return cell;
    }
    
}

-(void)tableView:(UITableView *)tableView1 didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView1 deselectRowAtIndexPath:indexPath animated:NO];
}


- (void)viewDidUnload
{
    [self setTable:nil];
    [self setArrayOfData:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (void)dealloc {
    [arrayOfData release];
    [table release];
    [super dealloc];
}
@end
